﻿Public Class perDepartamento
    Inherits perPadrao

#Region "Declaração de variáveis"
    Private objBuscaId As New perProximoId
#End Region

#Region "Propriedades"

    Public ReadOnly Property sqlConsulta() As String
        Get
            Dim sSql As String
            sSql = "Select IDDepartamento, Descricao From DC_Departamentos"
            Return sSql
        End Get
    End Property

#End Region

#Region "Métodos"

    Public Function inserirDepartamento(ByVal sDepartamento As String) As Int32

        Dim iIdDepartamento As Int32
        Dim sSql As String

        Try
            iIdDepartamento = Me.objBuscaId.BuscaID("IDDepartamento", "DC_Departamentoss")

            sSql = "  INSERT INTO DC_Departamentoss "
            sSql &= " ( "
            sSql &= "   IDDepartamento, "
            sSql &= "   Descricao "
            sSql &= " ) "
            sSql &= " VALUES "
            sSql &= " ( "
            sSql &= iIdDepartamento & ", "
            sSql &= "'" & sDepartamento & "'"
            sSql &= " ) "

            MyBase.executarAcao(sSql)
            Return iIdDepartamento
        Catch ex As Exception
            Throw New Exception("Exceção no método 'inserirDepartamento'. " & Environment.NewLine & Environment.NewLine & ex.Message)
        End Try

    End Function

    Public Sub alterarDepartamento(ByVal iIdDepartamento As Int32, _
                                   ByVal sDepartamento As String)

        Dim sSql As String

        Try

            sSql = "  UPDATE DC_Departamentoss SET "
            sSql &= "   Descricao = '" & sDepartamento & "'"
            sSql &= " WHERE "
            sSql &= "   IdDepartamento = " & iIdDepartamento

            MyBase.executarAcao(sSql)

        Catch ex As Exception
            Throw New Exception("Exceção no método 'alterarDepartamento'. " & Environment.NewLine & Environment.NewLine & ex.Message)
        End Try

    End Sub

    Public Sub excluirDepartamento(ByVal iIdDepartamento As Int32)

        Dim sSql As String

        Try
            sSql = "  DELETE FROM "
            sSql &= "   DC_Departamentoss "
            sSql &= " WHERE "
            sSql &= "   IDDepartamento = " & iIdDepartamento

            MyBase.executarAcao(sSql)

        Catch ex As Exception
            Throw New Exception("Exceção no método 'excluirDepartamento'. " & Environment.NewLine & Environment.NewLine & ex.Message)
        End Try

    End Sub

    Public Function selecionarCampoDepartamento(ByVal sCampo As String, _
                                                ByVal iIDDepartamento As Long) As DataTable
        Dim sSql As String
        Dim dsDados As New DataSet

        Try
            sSql = " SELECT " & sCampo
            sSql &= " FROM DC_Departamentoss "

            If (iIDDepartamento > 0) Then
                sSql &= " WHERE IDDepartamento = " & iIDDepartamento
            End If

            dsDados = MyBase.executarConsulta(sSql, "DC_Departamentoss")

            If Not dsDados Is Nothing AndAlso dsDados.Tables("DC_Departamentoss").Rows.Count > 0 Then
                Return dsDados.Tables("DC_Departamentoss")
            Else
                Return Nothing
            End If

        Catch ex As Exception
            Throw New Exception("Exceção no método 'selecionarCampoDepartamento'. " & Environment.NewLine & Environment.NewLine & ex.Message)
        End Try

    End Function

    Public Function selecionarDepartamento(ByVal iIdDepartamento As Int32) As DataRow
        Dim sSql As String
        Dim dsDados As New DataSet

        Try
            sSql = " SELECT * FROM DC_Departamentoss "
            sSql &= " WHERE IDDepartamento = " & iIdDepartamento

            dsDados = MyBase.executarConsulta(sSql, "DC_Departamentoss")

            If Not dsDados Is Nothing AndAlso dsDados.Tables("DC_Departamentoss").Rows.Count > 0 Then
                Return dsDados.Tables("DC_Departamentoss").Rows(0)
            Else
                Return Nothing
            End If

        Catch ex As Exception
            Throw New Exception("Exceção no método 'selecionarDepartamento'. " & Environment.NewLine & Environment.NewLine & ex.Message)
        End Try

    End Function
#End Region
End Class
